package com.alex.utils;


import com.alex.exceptions.CheckException;

import java.util.Collection;

public class CheckUtil {

    public static boolean isNotNull(String string) {
        return string != null && !"".equals(string) && string.length() > 0;
    }

    public static boolean checkList(Collection<?> list) {
        return list == null || list.size() == 0;
    }

    public static boolean isNull(String string) {
        return string == null || string.equals("");
    }


    public static void checkByZfl(boolean condition, String msgKey, Object... args) {
        if (condition) {
            fail(msgKey, args);
        }
    }

    public static void notEmpty(String str, String msgKey, Object... args) {
        if (str == null || str.isEmpty()) {
            fail(msgKey, args);
        }
    }

    /**
     * 如果是数字
     *
     * @param str
     * @return
     */
    public static Boolean isNumber(String str) {
        str = str.replaceAll("\\[pmid\\]", "");
        str = str.replaceAll("\\[PMID\\]", "");
        try {
            Integer.valueOf(str);
        } catch (Exception ex) {
            return false;
        }
        return true;
    }

    //如果是多个数字
    public static Boolean isNumberRange(String str) {
        str = str.replaceAll("\\[pmid\\]", "");
        str = str.replaceAll("\\[PMID\\]", "");
        String[] strs = str.split(",");
        for (String s : strs) {
            try {
                Integer.valueOf(s);
            } catch (Exception ex) {
                return false;
            }
        }
        return true;
    }

    public static Boolean isNumberRangeForKongGe(String str) {
        String[] strs = str.split(" ");
        for (String s : strs) {
            try {
                Integer.valueOf(s);
            } catch (Exception ex) {
                return false;
            }
        }
        return true;
    }

    public static Boolean isNumberRangeForFenHao(String str) {
        String[] strs = str.split(";");
        for (String s : strs) {
            try {
                Integer.valueOf(s);
            } catch (Exception ex) {
                return false;
            }
        }
        return true;
    }

    public static void check(boolean condition, String msgKey, Object... args) {
        if (!condition) {
            fail(msgKey, args);
        }
    }

    public static void check(boolean condition) {
        if (!condition) {
            fail("field.invalid");
        }
    }

    public static void notNull(Object obj, String msgKey, Object... args) {
        if (obj == null) {
            fail(msgKey, args);
        }
    }

    private static void fail(String msgKey, Object... args) {
        throw new CheckException(msgKey);
    }

    private static void fail(String msgKey) {
        throw new CheckException(msgKey);
    }


}
